package com.zhora.service.ai.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhora.common.domain.page.PageDataGridRespDTO;
import com.zhora.common.errcode.CommonCode;
import com.zhora.common.utils.NumberUtil;
import com.zhora.common.utils.ValidateUtil;
import com.zhora.db.common.util.PageDataGridRespUtil;
import com.zhora.db.common.util.PageQueryUtil;
import com.zhora.dto.ai.AiLibraryDocDTO;
import com.zhora.dto.ai.search.AiLibraryDocSearchDTO;
import com.zhora.entity.ai.AiLibraryDocEntity;
import com.zhora.mapper.ai.AiLibraryDocMapper;
import com.zhora.service.ai.IAiLibraryDocService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ai文档(ai_library_doc)表服务实现类
 *
 * @author zhehen.lu
 * @since 2025-09-15 17:45:22
 */
@Service
public class AiLibraryDocServiceImpl extends ServiceImpl<AiLibraryDocMapper, AiLibraryDocEntity> implements IAiLibraryDocService {

    /**
     * 根据条件分页查询ai文档列表
     *
     * @param searchDTO
     * @return {@link PageDataGridRespDTO< AiLibraryDocDTO>}
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public PageDataGridRespDTO<AiLibraryDocDTO> listPage(AiLibraryDocSearchDTO searchDTO) {
        ValidateUtil.notNull(searchDTO, CommonCode.PARMA_NOT_NULL);

        LambdaQueryChainWrapper<AiLibraryDocEntity> wrapper = getWrapper(searchDTO);

        PageQueryUtil<AiLibraryDocEntity, AiLibraryDocSearchDTO> pageQueryUtil = new PageQueryUtil<>(
                AiLibraryDocEntity.class,
                PageQueryUtil.TypeEnum.PARAM_PRIORITY_DEFAULT_PRIMARY,
                searchDTO
        );

        IPage<AiLibraryDocEntity> page = wrapper.page(pageQueryUtil.buildPageObj());

        return PageDataGridRespUtil.convert(page, AiLibraryDocDTO.class);
    }

    /**
     * 创建ai文档
     *
     * @param dto
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public void create(AiLibraryDocDTO dto) {
        ValidateUtil.notNull(dto, CommonCode.PARMA_NOT_NULL);
        AiLibraryDocEntity entity = BeanUtil.copyProperties(dto, AiLibraryDocEntity.class);

        save(entity);
    }

    /**
     * 依据ID获取ai文档详情
     *
     * @param id
     * @return {@link AiLibraryDocDTO}
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public AiLibraryDocDTO getDetailById(Long id) {
        AiLibraryDocSearchDTO searchDTO = new AiLibraryDocSearchDTO();
        searchDTO.setId(id);
        searchDTO.setDelFlag(Boolean.FALSE);

        return getDetail(searchDTO);
    }

    /**
     * 获取ai文档详情
     *
     * @param searchDTO
     * @return {@link AiLibraryDocDTO}
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public AiLibraryDocDTO getDetail(AiLibraryDocSearchDTO searchDTO) {
        LambdaQueryChainWrapper<AiLibraryDocEntity> wrapper = getWrapper(searchDTO);
        AiLibraryDocEntity entity = wrapper
                .last("LIMIT 1")
                .one();

        return BeanUtil.copyProperties(entity, AiLibraryDocDTO.class);
    }

    /**
     * 依据ID更新ai文档数据
     *
     * @param dto
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public void updateById(AiLibraryDocDTO dto) {
        ValidateUtil.notNull(dto, CommonCode.PARMA_NOT_NULL);
        AiLibraryDocEntity entity = BeanUtil.copyProperties(dto, AiLibraryDocEntity.class);

        updateById(entity);
    }

    /**
     * 获取ai文档列表
     *
     * @param searchDTO
     * @return {@link List< AiLibraryDocDTO>}
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    @Override
    public List<AiLibraryDocDTO> list(AiLibraryDocSearchDTO searchDTO) {
        ValidateUtil.notNull(searchDTO, CommonCode.PARMA_NOT_NULL);

        LambdaQueryChainWrapper<AiLibraryDocEntity> wrapper = getWrapper(searchDTO);

        List<AiLibraryDocEntity> entityList = wrapper.list();

        return BeanUtil.copyToList(entityList, AiLibraryDocDTO.class);
    }

    @Override
    public void removeByLibraryId(Long libraryId) {
        AiLibraryDocSearchDTO searchDTO = new AiLibraryDocSearchDTO();
        searchDTO.setLibId(libraryId);
        LambdaQueryChainWrapper<AiLibraryDocEntity> wrapper = getWrapper(searchDTO);

        remove(wrapper);
    }

    @Override
    public void removeById(Long docId) {
        AiLibraryDocSearchDTO searchDTO = new AiLibraryDocSearchDTO();
        searchDTO.setId(docId);
        LambdaQueryChainWrapper<AiLibraryDocEntity> wrapper = getWrapper(searchDTO);

        remove(wrapper);
    }

    /**
     * 获取ai文档的Wrapper对象
     *
     * @param searchDTO
     * @return {@link LambdaQueryChainWrapper< AiLibraryDocEntity>}
     * @date 2025-09-15 17:45:22
     * @author zhehen.lu
     */
    private LambdaQueryChainWrapper<AiLibraryDocEntity> getWrapper(AiLibraryDocSearchDTO searchDTO) {
        return lambdaQuery()
                .eq(NumberUtil.isGtZero(searchDTO.getId()), AiLibraryDocEntity::getId, searchDTO.getId())
                .eq(NumberUtil.isGtZero(searchDTO.getLibId()), AiLibraryDocEntity::getIdentify, searchDTO.getIdentify())
                .eq(StringUtils.isNoneEmpty(searchDTO.getName()), AiLibraryDocEntity::getName, searchDTO.getName())
                .eq(StringUtils.isNoneEmpty(searchDTO.getIdentify()), AiLibraryDocEntity::getIdentify, searchDTO.getIdentify())
                .eq(null != searchDTO.getDelFlag(), AiLibraryDocEntity::getDelFlag, searchDTO.getDelFlag());
    }

}
